﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.Sql;
using System.Data.SqlClient;

public class MovieRepository
{
    SqlConnection con;

	public MovieRepository()
	{
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["MovieBuddies"].ConnectionString);
        con.Open();
    }

    public int CreateNewUser(string Username, string Password, string Email)
    {
        string sqlStatement = String.Format("INSERT INTO [User] (Username, Password, Email, CreationDate) VALUES ('{0}', '{1}', '{2}', '{3}')", new string[] { Username, Password, Email, DateTime.Now.ToShortDateString()});
        SqlCommand command = new SqlCommand(sqlStatement, con);
        return command.ExecuteNonQuery();
    }

    public List<User> GetUsers()
    {
        string sqlStatement = "SELECT UserID, Username, Password, Email FROM [User]";
        SqlCommand command = new SqlCommand(sqlStatement, con);

        SqlDataReader reader = command.ExecuteReader();

        List<User> users = new List<User>();
        while(reader.Read())
        {
            users.Add(new User((int)reader["UserID"], reader["Username"].ToString(), reader["Password"].ToString(), reader["Email"].ToString()));
        }
        reader.Close();
        return users;
    }
}
